package cn.fl.capital.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.capital.dto.CapFundOutLogDTO;
import cn.fl.capital.dto.CapFundVfLogRDTO;
import cn.fl.capital.condition.CapFundOutLogQueryCondition;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.service.ICapFundOutLogApiService;
import cn.fl.capital.service.ICapFundOutLogService;
import cn.fl.capital.transform.CapFundOutLogVOTransform;
import cn.fl.capital.transform.CapFundVfLogRVOTransform;
import cn.fl.capital.vo.CapFundOutLogVO;
import cn.fl.capital.vo.CapFundVfLogRVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;

import java.util.List;
import java.util.Map;

/**
 * @Description: 客户资金转出记录表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapFundOutLogServiceImpl implements ICapFundOutLogService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(CapFundOutLogServiceImpl.class);
	
	/**
	 * 客户资金转出记录表 暴露服务
	 */
	@Autowired
	private ICapFundOutLogApiService capFundOutLogApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public CapFundOutLogVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		CapFundOutLogDTO dto = this.capFundOutLogApiService.selectCapFundOutLogById(id);
		return CapFundOutLogVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapFundOutLogVO entity) throws BaseException{
		if(null == entity) return null;
		return this.capFundOutLogApiService.addCapFundOutLog(CapFundOutLogVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.capFundOutLogApiService.removeCapFundOutLogByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(CapFundOutLogVO entity) throws BaseException{
		if(null == entity) return false;
		return this.capFundOutLogApiService.modifyCapFundOutLog(CapFundOutLogVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapFundOutLogQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapFundOutLogQueryCondition());
        }
		condition=(CapFundOutLogQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capFundOutLogApiService.searchCapFundOutLogListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapFundOutLogVO> voList=CapFundOutLogVOTransform.toVOList((List<CapFundOutLogDTO>)page.getResultList());
            this.setDictData(voList);//设置数据字典
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<CapFundOutLogVO> searchList(CapFundOutLogQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapFundOutLogQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<CapFundOutLogVO> voList=CapFundOutLogVOTransform.toVOList(this.capFundOutLogApiService.searchCapFundOutLogList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<CapFundOutLogVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
			 Map<String, String> CdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapitalConstant.CODE_CAP_OUT_TYP_CD);
	         for (CapFundOutLogVO capFundOutLogVO : voList) {
	         	 if (CdMap != null && CdMap.containsKey(capFundOutLogVO.getOutTypCd())) {
	         		 capFundOutLogVO.setOutTypCdNm(CdMap.get(capFundOutLogVO.getOutTypCd()));
	              }
			}	
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

	@Override
	public List<CapFundOutLogVO> searchVList(List<Long> idsList) throws BaseException {
		List<CapFundOutLogVO> voList =null;
		if(CollectionUtils.isNotEmpty(idsList)){
		      voList = CapFundOutLogVOTransform.toVOList(this.capFundOutLogApiService.searchOutLogRList(idsList));
              Map<String, String> CdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapitalConstant.CODE_CAP_OUT_TYP_CD);
		            for (CapFundOutLogVO capFundOutLogVO : voList) {
		            	 if (CdMap != null && CdMap.containsKey(capFundOutLogVO.getOutTypCd())) {
		            		 capFundOutLogVO.setOutTypCdNm(CdMap.get(capFundOutLogVO.getOutTypCd()));
		                 }
					}
		}
		 return voList;
	}
	
	
	
	    
}
